from tkinter import *
import tkinter.messagebox
import requests
from threading import Thread
from bs4 import BeautifulSoup

def movie_game_description(root2,s):
    root4 = Toplevel()
    root4.title("最火游戏与电影")
    # 设置窗口大小
    convas = tkinter.Canvas(root4, height=500, width=500)
    root4.geometry('500x500+400+150')

    def exit():
        root4.destroy()

    sb = Scrollbar(root4)
    lb = Listbox(root4, yscrollcommand=sb.set, bg='#F7FAF2',exportselection=False)
    lb.place(x=10, y=10, width=480, height=400)
    sb.config()

    def gamesList():
        lb.delete(0,END)
        print("正在获取游戏信息,请稍候...")
        url = "http://www.doyo.cn/rank/wangluo/"
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36"
        }

        request = requests.get(url,params=headers).text
        soup = BeautifulSoup(request,'html.parser')

        nameList = soup.find_all(class_="name")
        for na in nameList:
            print(na.get_text())
            lb.insert(END,na.get_text())


    def craw_main(url):
        header= {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}

        html = requests.get(url,headers=header).text

        soup = BeautifulSoup(html,"lxml")

        ol = soup.find("ol",class_="grid_view")
        lis =ol.find_all("li")
        for li in lis:
            # 编号
            num = li.find('em',class_='').get_text()
            # 名称
            title = li.find('span',class_='title').get_text()
            # 评分
            score = li.find('span',class_='rating_num').get_text()
            # 导演
            director = li.find('p',class_='')
            director = director.get_text()
            director = director.replace(" ","").replace("\n","")
            director = director.replace("\xa0","").replace("\xee","").replace("\xf6","").replace("\u0161","").replace("\xf4","")

            info = str(num)+','+title+','+score+'\r\n'+director+'\r\n'
            lb.insert(END,info)

    def movieList():
        lb.delete(0,END)
        print("正在获取电影信息,请稍候...")
        num = 2
        page=0
        while page<=num*10:
            url='https://movie.douban.com/top250?start=%d&filter='%page
            craw_main(url)
            page += 5

    moviebtn = Button(root4, text='电影信息', bg="#E8EDD8",font=("微软雅黑", 14),command=movieList)
    moviebtn.place(x=100, y=430, width=100, height=50)

    gamesbtn = Button(root4, text='最火游戏', bg="#E8EDD8",font=("微软雅黑", 14), command=gamesList)
    gamesbtn.place(x=200, y=430, width=100, height=50)

    quitbtn = Button(root4, text='安全退出', bg="#E8EDD8",font=("微软雅黑", 14), command=exit)
    quitbtn.place(x=300, y=430, width=100, height=50)
        # 启动消息循环
    root4.mainloop()